package view;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;

import controller.ACSystem;

import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.border.LineBorder;

import model.Auction;
import model.AuctionCalendar;
import model.Bidder;
import model.Item;

import model.WestPanelEnum;

import java.awt.Color;

/**
 * 
 * @author Bob Landowski
 *
 */
@SuppressWarnings({"serial", "static-access"})
public class EastPanelBidder extends JPanel {

	/** The ACcontroller */
	private ACSystem my_ACcontroller;

	/** The main frame */
	private JFrame my_mainframe;
	
	/** The bidder */
	private Bidder my_bidder = new Bidder(null, null, null, null, null, null, null);
	
	/** The auction selected */
	private String auctionSelected;
	
	/** The auction searched */
	private Auction auctionSearched;
	
	/** The auction calendar */
	private AuctionCalendar my_auction_calendar;
	
	/**
	 * Create the panel.
	 * 
	 * @author Bob Landowski
	 * @author Eric Struck
	 */
	public EastPanelBidder(final ACSystem the_ACcontroller) {
		setBorder(new LineBorder(new Color(0, 0, 0), 5));
		my_ACcontroller = the_ACcontroller;
		my_auction_calendar = my_ACcontroller.getAuctionCalendar();
		setLayout(null);
		setPreferredSize(new Dimension(180, 400));

		
		JButton calendarButton = new JButton("Calendar");
		calendarButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				my_ACcontroller.setWestPanel(WestPanelEnum.CALENDAR); 
				my_ACcontroller.getMainFrame().westPanelState(new Object ());
				}
		});
		calendarButton.setBounds(15, 28, 148, 29);
		add(calendarButton);

		JButton listAuctionsButton = new JButton("List Auctions");
		listAuctionsButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {

				Object[] possibilities = new Object[my_ACcontroller
						.getAuctionList().size()];
				for (int i = 0; i < my_ACcontroller.getAuctionList().size(); i++) {
					possibilities[i] = my_ACcontroller.getAuctionList().get(i)
							.getName();
				}

				auctionSelected = (String) JOptionPane.showInputDialog(
						my_mainframe, "Choose your Auction", "Auction chooser",
						JOptionPane.PLAIN_MESSAGE, null, possibilities, null);
				if (auctionSelected != null) {	
					auctionSearched = my_auction_calendar.findAuction(auctionSelected);	
					my_ACcontroller.setWestPanel(WestPanelEnum.AUCTION);
					my_ACcontroller.getMainFrame().westPanelState(auctionSearched);
				}
			}

		});
		listAuctionsButton.setBounds(15, 86, 148, 29);
		add(listAuctionsButton);

		JButton exitButton = new JButton("Exit");
		exitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);

			}
		});
		exitButton.setBounds(15, 332, 148, 29);
		add(exitButton);

		JButton bidHistoryButton = new JButton("Bid History");
		bidHistoryButton.addActionListener(new ActionListener() {
			

			public void actionPerformed(ActionEvent e) {
				my_bidder = (Bidder)my_ACcontroller.getCurrentUser();
				if(!my_bidder.findBids().equals(null)){
				Object[] possibilities = new Object[my_bidder.findBids().size()];
				
				for (int i = 0; i < my_bidder.findBids().size(); i++) {
					possibilities[i] = my_bidder.findBids().get(i).toString();
				}
				
				String s = (String) JOptionPane.showInputDialog(my_mainframe,
						null, "Bid History",
						JOptionPane.PLAIN_MESSAGE, null, possibilities, null);
				if (s == null) {
					
				} else {
				
				String[] words = s.split("~");
				int x = 0;
				if (my_ACcontroller.isNumeric(words[0])) {
					x = (int) Double.parseDouble(words[0]);
				}
				Item item = null;
				boolean found = false;
				for (int i = 0; i < my_ACcontroller.getMy_auctions().size(); i++){
					for(int j = 0; j < my_ACcontroller.getMy_auctions().get(i).getItemList().size(); j++) {
						item = my_ACcontroller.getMy_auctions().get(i).getItemList().get(j);
						if (item.getMyItemNumber() == x) {
							// item found:
							found = true;
							break;
						}
					}
					if (found) break;
				} // end of for-loop
				
				my_ACcontroller.setWestPanel(WestPanelEnum.ITEM);
				my_ACcontroller.getMainFrame().westPanelState(item);
				}
				}else{
				
				JOptionPane.showMessageDialog(my_mainframe,
					    "There is no Bid history",
					    null,
					    JOptionPane.ERROR_MESSAGE);
				}
				
			}
			
		});

		bidHistoryButton.setBounds(15, 146, 148, 29);
		add(bidHistoryButton);

	}

	/** The getObjectData method */
	public void getObjectData(Object the_object) {
		
	}
}
